﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FimetContoDeposito.DAL;
using GrayParrot.Core;

namespace FimetContoDeposito.BLL
{
    public class RepoBase<T> : Repository<T, Guid> 
        where T : class, IReporitoryObject<Guid>
    {
        public RepoBase()
        {

        }

        public ReturnValue<bool> Add(T item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                if (item.Id == Guid.Empty)
                {
                    item.Id = Guid.NewGuid();
                }

                base.Add(item);
                base.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<bool> Edit(T item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                base.Edit(item);
                base.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<bool> Delete(T item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                base.Delete(item);
                base.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<bool> Delete(Guid idArticolo)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                ReturnValue<T> obj = this.Get(idArticolo);
                if (obj.HasValue)
                {
                    retValue = this.Delete(obj.Value);
                }
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<T> Get(Guid id)
        {
            ReturnValue<T> retValue = new ReturnValue<T>();

            try
            {
                retValue.Value = base.Get(id);
                retValue.HasValue = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<IQueryable<T>> GetAll()
        {
            ReturnValue<IQueryable<T>> retValue = new ReturnValue<IQueryable<T>>();
            retValue.HasValue = false;
            retValue.Value = null;

            try
            {
                retValue.Value = base.GetAll();
                retValue.HasValue = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }
    }
}
